//
//  HomeRankingCell.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 2017/5/17.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "HomeRankingCell.h"

@interface HomeRankingCell()

@property(nonatomic,strong)UIView     *topGrayView; //大家都关注
@property(nonatomic,strong)UIView     *lineView;

@property(nonatomic,strong)UIView     *titleView; //热销排行
@property(nonatomic,strong)UILabel    *title;

@property(nonatomic,strong)UIView     *btnView;
@property(nonatomic,strong)UIButton   *selectBtn;
@property(nonatomic,strong)NSMutableArray *btnArray;


@end

@implementation HomeRankingCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self buildSubViews];
    }
    return self;
}

- (void)buildSubViews
{
    [self.contentView addSubview:self.topGrayView];
    [self.topGrayView addSubview:self.lineView];
    
    [self.contentView addSubview:self.titleView];
    [self.titleView addSubview:self.title];
    
    [self.contentView addSubview:self.btnView];
    
    CGFloat leftMargin = 15;
    CGFloat centerMargin = 10;
    CGFloat ViewW = (WIDTH - leftMargin*2 - centerMargin*2)/3;
    
    for (int i = 0; i < 3; ++i) {
        
        UIImageView *toolView = [[UIImageView alloc] init];
        toolView.image = [UIImage imageNamed:[NSString stringWithFormat:@"bg_recommend%d",i]];
        toolView.userInteractionEnabled = true;
        toolView.tag = i;
        [toolView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rankingClick:)]];
        [self.contentView addSubview:toolView];
        
        toolView.frame = CGRectMake(leftMargin + i*(ViewW+centerMargin),75,ViewW,140);
    }
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0,226,WIDTH,1)];
    line.backgroundColor = [UIColor lightGrayColor];
    line.alpha= 0.2f;
    [self.contentView addSubview:line];
    
    CGFloat margin = WIDTH/30;
    CGFloat btnW = (WIDTH-leftMargin*2-margin*5)/6;
    CGFloat btnH = 30;
    
    for (int i = 0; i < self.titleArray.count; ++i) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.btnView addSubview:button];
        
        [button setTitle:self.titleArray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        button.titleLabel.font = FONT(13);
       
        button.layer.masksToBounds = true;
        button.layer.cornerRadius = 3;
        button.tag = i;
        
        [self.btnArray addObject:button];
        if (i == 0) {
            button.selected = true;
            self.selectBtn = button;
            [button setBackgroundColor:DEFAULT_COLOR];
        }
        
        int maxCol = 6;
        CGFloat col = i % maxCol;
        CGFloat row = i / maxCol;
        
        button.frame = CGRectMake(15+col*(margin+btnW),10+row*(btnH+5),btnW,btnH);
        [button addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
}

- (NSArray <NSString *>*)titleArray
{
    return @[@"热门",@"景宁",@"松阳",@"遂昌",@"云和",@"青田",@"龙泉",@"缙云",@"2日游",@"画乡",@"金矿",@"哈喽",];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat leftMargin = 15;
    
    self.topGrayView.frame = CGRectMake(0,0,WIDTH,35);
    WeakSelf(self);
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(weakself.topGrayView);
        make.width.mas_equalTo(WIDTH-leftMargin*2);make.height.mas_equalTo(1);
    }];
    
    self.titleView.frame = CGRectMake(0,CGRectGetMaxY(_topGrayView.frame),WIDTH,40);
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakself.titleView);
        make.left.equalTo(weakself.titleView.mas_left).offset(leftMargin);
    }];

    self.btnView.frame = CGRectMake(0,226,WIDTH,85);
}

- (void)rankingClick:(UIGestureRecognizer *)recognizer
{
    if (self.rankingBlock) {
        self.rankingBlock(recognizer.view.tag);
    }
}

- (void)titleBtnClick:(UIButton *)button
{
    button.selected = true;
    [button setBackgroundColor:DEFAULT_COLOR];
    [self.selectBtn setBackgroundColor:[UIColor whiteColor]];
    self.selectBtn.selected = false;
    self.selectBtn = button;
    
    if (self.titleBlock) {
        self.titleBlock(button.tag);
    }
}


#pragma mark lazy
- (UIView *)topGrayView
{
    if (!_topGrayView) {
        _topGrayView = [[UIView  alloc] init];
        _topGrayView.backgroundColor = BACKGROUND_COLOR;
    }
    return _topGrayView;
}

- (UIView *)lineView
{
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor lightGrayColor];
        _lineView.alpha= 0.2f;
    }
    return _lineView;
}

- (UIView *)titleView
{
    if (!_titleView) {
        _titleView = [[UIView alloc] init];
    }return _titleView;
}

- (UILabel *)title
{
    if (!_title) {
        _title = [[UILabel alloc] init];
        _title.text = @"热销排行";
        _title.font = BOLDFONT(15);
        _title.textAlignment = NSTextAlignmentLeft;
    }return _title;
}

- (UIView *)btnView
{
    if (!_btnView) {
        _btnView = [[UIView alloc] init];
//        _btnView.backgroundColor = BACKGROUND_COLOR;
    }
    return _btnView;
}

@end
